#pragma once
#include<iostream>
#include<assert.h>

using namespace std;

namespace lyz
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;

		vector()
		:_start(nullptr)
		,_finish(nullptr)
		,_end_of_storage(nullptr)
		{

		}

		size_t size()const
		{
			return (_finish - _start);
		}

		size_t capacity() const
		{
			return(_end_of_storage - _start);
		}

		void reserve(int n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start,sizeof(T)* size());
					delete[] _start;
				}

				_start = tmp;
				_finish = tmp + sz;
				_end_of_storage = _start + n;
			}
			

		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
		}
		T operator[](size_t pos)
		{
			assert(pos < size());
			return *(_start + pos);
		}



	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;

	};

	void testVector()
	{
		lyz::vector<int> v1;

		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);

		for (int i = 0; i < v1.size(); i++)
		{
			cout << v1[i] << " ";
		}
	}
}